(function(namespace) {
	$.fn.XDialog = function(options) {
		this.data("XDialog", new Dialog(options, this));// 可以存储进去
		return this;
	};

	/**
	 * 样式
	 */
	var cssClass = {
		xdialog_all : "xdialog_all",
		xdialog_header : "xdialog_all xdialog_header",
		xdialog_body : "xdialog_all xdialog_body",
		xdialog_footer : "xdialog_all xdialog_footer",
		xdialog_button : "xdialog_button"
	};

	/**
	 * 对话框对象
	 */

	function Dialog(options, dom) {
		options = options || {};
		var self = this;
		this.buttons = [];
		var property = initUI.call(this, options, dom);
		initMoveable.call(this, property);
		
		/**
		 * 添加button 支持数组和单个对象
		 */
		this.addButton = function(option) {
			var addButton = function(option) {
				var btn = $("<input type='button'/>").appendTo(property.footer)
						.addClass(cssClass.xdialog_button).attr("value",
								option["text"]);
				btn.bind("click", option["click"]);
				self.buttons.push(btn);
			};
			if ($.isArray(option)) {
				$(option).each(function() {
					addButton(this);
				});
			} else {
				addButton(option);
			}
		};
		
		this.setTitle = function(title){
			
		};

		if (options["buttons"]) {
			this.addButton(options["buttons"]);
		}
		
	}
	/**
	 * 初始化dom
	 */
	function initUI(options, dom) {
		var alldiv = $("<div></div>").addClass(cssClass.xdialog_all).appendTo(
				dom);
		alldiv.bindResize({
			callback : function(){
				body.height(alldiv.height()-header.height()-footer.height());
			}
		});
		var header = $("<div></div>").addClass(cssClass.xdialog_header)
				.appendTo(alldiv);
		var body = $("<div></div>").addClass(cssClass.xdialog_body).appendTo(
				alldiv);
		var footer = $("<div></div>").addClass(cssClass.xdialog_footer)
				.appendTo(alldiv);
		body.height(alldiv.height()-header.height()-footer.height());
		return {
			alldiv : alldiv,
			header : header,
			body : body,
			footer : footer
		};
		// alldiv.height(header.height()+body.height()+footer.height());
	}
	/**
	 *让dialog可以移动
	 */
	function initMoveable(property) {
		var alldiv = property.alldiv, header = property.header;
		header.css("cursor", "move");
		var start = {
			top : 0,
			left : 0
		};
		var mouse_up = function() {
			$(document.body).unbind(".move");
			
		};
		var mouse_move = function(evt) {
			var top = evt.pageY-start.top;
			var left = evt.pageX-start.left;
			alldiv.offset({
				top:top,
				left:left
			});
		};
		
		var mouse_down = function(evt) {
			var offset = alldiv.offset();
			start.top = evt.pageY - offset.top;
			start.left = evt.pageX - offset.left;
			$(document.body).bind("mousemove.move", mouse_move);//body绑定事件比较流畅
			header.bind("mouseup.move",mouse_up);
		};
		header.bind("mousedown", mouse_down);
	}
	// var xdialog = {
	// name:"123"
	// };
	// namespace["XDialog"] = xdialog;

})(window);